<?php
/**
 * 状态管理
 * User: shijy
 * Date: 2016/3/21
 * Time: 10:10
 */


namespace Admin\Controller;
use Admin\BaseController;
use Common\Model\BusinessModel;
use Common\Model\RoomModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Think\Controller;
use Think\Db;

class StatusController extends BaseController
{

    /**
     * 门店列表
     */
    public function statusList(){
        if(IS_POST){
            $timeSTMP=i('post.time',0,'intval');
            if($timeSTMP ==0) {
                $today = date('Y-m-d');
                $timeSTMP = strtotime($today);
            }
            $weekarray_show=array("日","一","二","三","四","五","六");

            $w = date("w",$timeSTMP);

            if($w!=0){
                $startTime = strtotime(0-$w." days",$timeSTMP);
            }
            else{
                $startTime = $timeSTMP;
            }
            $startDate=date('Y-m-d',$startTime);

            ///////////////////////////////////////////////////
            $endDate=date('Y-m-d',$startTime+UtilsModel::ONE_DAY * 13);

            $rooms=M('r_room',null,DB_MAIN_CFG)->where('status='.RoomModel::ROOM_ONLINE)->field('rid,roomName')->select();
            $list=array();
            foreach($rooms as $room){
                $room['data']=array();
                $list[$room['rid']]=$room;
            }


            $startTmp   =strtotime($startDate);
            $endTmp     =strtotime($endDate);


            $dateArr=array();
            for($t=$startTmp;$t<=$endTmp;$t+=UtilsModel::ONE_DAY){
                $tmpDate=date('Y-m-d',$t);
                $dateArr[]=$tmpDate;
                $w=date('w',$t);

                switch ($w){
                    case 0:
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                        $isWorkDay=true;
                        break;
                    case 5:
                    case 6:
                        $isWorkDay=false;
                        break;
                }
                foreach ($list as $rid=>$room){
                    if ($isWorkDay){
                        $v=array('w',RoomModel::ROOM_STATUS_IDLE);
                    }else{
                        $v=array('e',RoomModel::ROOM_STATUS_IDLE);
                    }
                    $list[$rid]['data'][$tmpDate] = $v;
                }
            }

            $status=M('r_status',null,DB_MAIN_CFG)->where("date between '{$startDate}' and '{$endDate}'")->order('rid,date')->select();
            foreach ( $status as $special){
                $list[$special['rid']]['data'][$special['date']][1]=$special['status'];
            }

            $this->smarty->assign('week',$weekarray_show);
            $this->smarty->assign('dateArr',$dateArr);
            $this->smarty->assign('list',$list);

            $statusKv=RoomModel::getStatusKv();
            $this->smarty->assign('statusKv',$statusKv);

            $html=$this->smarty->fetch('Status/status_list_do.html');
            $this->ajaxReturn(array('status'=>0,'HTML'=>$html,'time'=>$startTime));
        }
        else{
            $roomStatus=RoomModel::getStatusKv();
            $this->smarty->assign('roomStatus',$roomStatus);

            $this->smarty->display('Status/status_list.html');
        }


    }


    public function setSpecial(){
        $rid=I('post.rid',0,'intval');
        $startDate=I('post.startDate');
        $endDate=I('post.endDate');
        $status=I('post.status',0,'intval');
        if($rid<=0){
            $this->ajaxReturn(array('status'=>1,'msg'=>'请指定房间'));
        }

        $startTime  = strtotime($startDate);
        $endTime    = strtotime($endDate);
        if($startTime==0 || $endTime==0){
            $this->ajaxReturn(array('status'=>1,'msg'=>'日期范围错误'));
        }


            $data = array();
            for ($tmp = $startTime; $tmp <= $endTime; $tmp += UtilsModel::ONE_DAY) {
                $data[] = array(
                    'rid' => $rid,
                    'date' => date('Y-m-d', $tmp),
                    'status' => $status
                );
            }
            if (count($data) > 0) {
                M('r_status', null, DB_MAIN_CFG)->addAll($data, null, true);
            }
        $this->ajaxReturn(array('status'=>0,'msg'=>'success'));
    }

    /**
     * 房屋增加
     */
    public function roomAdd(){

        if(IS_POST){
            $nickname=I('post.roomName');
            $restaurantItem = M('r_room',null,DB_MAIN_CFG)->where("roomName like '{$roomName}'")->find();
            if($restaurantItem){
                $this->error('同名房间已经存在');
            }

            $album=I('post.album');
            if(count($album)<1){
                $this->error('图集至少需要一张图片');
            }

            $userM = M('r_room',null,DB_MAIN_CFG);
            $rules = array(
                array('roomName', 'require', '门店名称必须填写'),
                array('listPic', 'require','列表图必须上传'),
            );

            if($data = $userM->validate($rules)->create()){

                $data['album']=implode(',',$album);
                 $userMId=$userM->add($data);
                 $this->success('添加成功');
            }else{
                $this->error($userM->getError());
            }
        }
        else{

            $this->smarty->display('Room/room_edit.html');
        }
    }

    /**
     * 房屋编辑
     */
    public function roomEdit(){
        if(IS_POST){
            $rid=I('post.rid',0,'intval');
            $roomName=I('post.roomName');
            $restaurantItem = M('r_room',null,DB_MAIN_CFG)->where("roomName like '{$roomName}' and rid<>{$rid}")->find();
            if($restaurantItem){
                $this->error('同名房间已经存在');
            }

            $album=I('post.album');
            if(count($album)<1){
                $this->error('图集至少需要一张图片');
            }

            $userM = M('r_room',null,DB_MAIN_CFG);
            $rules = array(
                array('roomName', 'require', '房间名称必须填写'),
                array('listPic', 'require','列表图必须上传'),
            );

            if($data = $userM->validate($rules)->create()){

                $data['album']=implode(',',$album);
                $userMId=$userM->where("rid={$rid}")->save($data);
                $this->success('修改成功');
            }else{
                $this->error($userM->getError());
            }
        }
        else{
            $rid=I('get.rid',0,'intval');
            $uinfo=M('r_room',null,DB_MAIN_CFG)->find($rid);

            $this->smarty->assign('obj',$uinfo);

            $this->smarty->display('Room/room_edit.html');
        }
    }

    /**
     *
     */
    public function roomUpdate(){
        $uid = I('get.uid', null, 'intval');
        $userM = M('u_user',null,DB_MAIN_CFG);
        $item = $userM->find($uid);
        if(empty($item)){
            $this->error('没有存在该用户');
        }
        if($item['status']!=UserModel::AUDIT_PASS) {
            $result = $userM->where('uid=' . $uid)->setField('status', UserModel::AUDIT_PASS);
        }else{
            $result = $userM->where('uid=' . $uid)->setField('status', UserModel::AUDIT_UNPASS);
        }
        if ($result) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }


}
